package song.NO_14;

public class LongestCommonPrefix {
    private String[] strings;
    private String result;
    public LongestCommonPrefix(){
        this.strings = new String[]{""};
        this.result = "";
    }
    public LongestCommonPrefix(String[] strings){
        this.strings = strings;
        this.result = "";
    }
    public void Finding(){
        StringBuilder temp = new StringBuilder(this.strings[0]);
        for (int i = 1; i != this.strings.length; ++i){
            var str = this.strings[i];
            for (int j = 0; j != str.length(); ++j){
                if (j == temp.length()) break;
                if (str.charAt(j) != temp.charAt(j)){
                    temp.delete(j, temp.length());
                    break;
                }
            }
        }
        this.result = temp.toString();
        if (this.result.length() == 0) this.result = "null";
    }
    public void print(){
        System.out.println(this.result);
    }
    public static void main(String[] args){
        String[] s = {"flower", "flow", "flight"};
        String[] s2 = {"dog","racecar","car"};
        LongestCommonPrefix l1 = new LongestCommonPrefix(s2);
        l1.Finding();
        l1.print();
    }
}
